package com.foton.lotterydomain.activity.service.partake;

import cn.hutool.core.util.ObjectUtil;
import com.foton.lotterycommon.common.Constants;
import com.foton.lotterycommon.common.Result;
import com.foton.lotterydomain.activity.model.req.PartakeReq;
import com.foton.lotterydomain.activity.model.res.PartakeRes;
import com.foton.lotterydomain.activity.model.vo.ActivityBillVO;
import com.foton.lotterydomain.activity.model.vo.DrawOrderVO;
import com.foton.lotterydomain.activity.model.vo.UserTakeActivityVO;

/**
 * @Author RenPu
 * @Date 2024/1/2 15:04
 * @Version 1.0
 * @Description: 业务中间编排曾，扩展层
 **/
public abstract class BaseActivityPartake extends ActivityPartakeSupport implements IActivityPartake {






    @Override
    public PartakeRes doPartake(PartakeReq req) {

        UserTakeActivityVO userTakeActivityVO= this.queryNoConsumedTakeActivityOrder(req.getActivityId(),String.valueOf(req.getUserId()));
        if(ObjectUtil.isNotEmpty(userTakeActivityVO)){
            return buildPartakeResult(userTakeActivityVO.getStrategyId(),userTakeActivityVO.getTakeId());
        }


        //查询活动账单
        ActivityBillVO activityBillVO = super.queryActivityBill(req);

        // 活动信息校验处理【活动库存、状态、日期、个人参与次数】
        Result checkResult = this.checkActivityBill(req,activityBillVO);
        if(!Constants.ResponseCode.SUCCESS.getCode().equals(checkResult.getCode())){
            return new PartakeRes(checkResult.getCode(),checkResult.getInfo());
        }

        // 扣减活动库存【目前为直接对配置库中的 lottery.activity 直接操作表扣减库存，后续优化为Redis扣减】
        Result stockDownResult = this.subtractionActivityStock(req);
        if(!Constants.ResponseCode.SUCCESS.getCode().equals(stockDownResult.getCode())){
            return new PartakeRes(stockDownResult.getCode(),stockDownResult.getInfo());
        }


        // 领取活动信息【个人用户把活动信息写入到用户表】
        Result reciveResult =this.grabActivity(req,activityBillVO);
        if(!Constants.ResponseCode.SUCCESS.getCode().equals(reciveResult.getCode())){
            return new PartakeRes(reciveResult.getCode(),reciveResult.getInfo());
        }

        // 封装结果【返回的策略ID，用于继续完成抽奖步骤】
        PartakeRes partakeRes = new PartakeRes(Constants.ResponseCode.SUCCESS.getCode(),Constants.ResponseCode.SUCCESS.getInfo());
        partakeRes.setStrategyId(activityBillVO.getStrategyId());
        return partakeRes;
    }


    protected  PartakeRes buildPartakeResult(Long strategyId, Long takeId){
        PartakeRes partakeRes = new PartakeRes(Constants.ResponseCode.SUCCESS.getCode(),Constants.ResponseCode.SUCCESS.getInfo());
        partakeRes.setStrategyId(strategyId);
        partakeRes.setTakeId(takeId);
        return partakeRes;
    };




    /**
     * 活动信息校验处理，把活动库存、状态、日期、个人参与次数
     *
     * @param partake 参与活动请求
     * @param bill    活动账单
     * @return 校验结果
     */
    protected abstract Result checkActivityBill(PartakeReq partake, ActivityBillVO bill);



    /**
     * 扣减活动库存
     *
     * @param req 参与活动请求
     * @return 扣减结果
     */
    protected abstract Result subtractionActivityStock(PartakeReq req);

    /**
     * 领取活动
     *
     * @param partake 参与活动请求
     * @param bill    活动账单
     * @return 领取结果
     */
    protected abstract Result grabActivity(PartakeReq partake, ActivityBillVO bill);


    /**
     * 查询是否存在未执行抽奖领取活动单【user_take_activity 存在 state = 0，领取了但抽奖过程失败的，可以直接返回领取结果继续抽奖】
     * @param activityId
     * @param uId
     * @return
     */

    protected abstract UserTakeActivityVO queryNoConsumedTakeActivityOrder(Long activityId, String uId);


}
